<?php

/**
 * login actions.
 *
 * @package    lynx4
 * @subpackage login
 * @author     David Quinones - dquinones@aberic.com
 */
class loginActions extends sfActions
{
    /**
     * Preexecute
     */
    public function preExecute ()
    {
        //$this->setLayout('layoutLogin');
    }
    /**
     * Index Login
     * @param sfWebRequest $request
     */
    public function executeIndex(sfWebRequest $request)
    {
        $this->redirectIf($this->getUser()->isAuthenticated(),'@default_index?module=home');
        $this->login_user = new LoginForm();
        
        if ($request->isMethod('post'))
        {
            $this->login_user->bind($request->getParameter('wdLogin'));
            if ($this->login_user->isValid())
            {
                $this->getUser()->setAttribute('id_user_seller', $this->login_user->dataUser->getIdUser());
                $this->getUser()->setAttribute('id_profile',$this->login_user->dataUser->getIdProfile());
                $this->getUser()->setAttribute('login_user', $this->login_user->dataUser->getLogin());
                $this->getUser()->setAttribute('name_user',$this->login_user->dataUser->getNameUser());
                $this->getUser()->setAttribute('email_user',$this->login_user->dataUser->getEmail());
                //Credencial de administrador
                if($this->login_user->dataUser->getIdUser()==1) {
                    $this->getUser()->addCredential('adminSeller');
                }
                //Autentica al usuario
                $this->getUser()->setAuthenticated(true);
                $this->redirect('@default_index?module=home');
            }
        }
    }
    /**
     * Close session
     */
    public function executeClose() {
        //Elimina todas las credenciales
        $this->getUser()->clearCredentials();
        // Culmina la sesion del usuario
        $this->getUser()->getAttributeHolder()->clear();
        // Desactiva la autenticacion del usuario
        $this->getUser()->setAuthenticated(false);
        // Direcciona al login
        $this->redirect('@default_index?module=login');
    }
    
    
}
